package com.xijia.wuyouwallet.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.telephony.TelephonyManager;
import android.util.Base64;

import com.xijia.wuyouwallet.config.FusionCode;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * Created by Administrator on 2016/9/29 0029.
 */

public class StringUtils {
    /**
     * 截取手机号为123****4234的形式
     *
     * @param phone
     * @return
     */
    public static String hintPhone(String phone) {
        String str1 = phone.substring(0, 3);
        String str2 = phone.substring(7, 11);
        return str1 + "****" + str2;
    }
    /**
     *
     *
     * @param num
     * @return
     */
    public static String hintBankNum(String num) {
        if (num.length()==16){
            String str1 = num.substring(0, 4);
            String str2 = num.substring(12, num.length());
            return str1 + "********" + str2;
        }else if (num.length()==19){
            String str1 = num.substring(0, 4);
            String str2 = num.substring(15, num.length());
            return str1 + "********" + str2;
        }else {
            return num;
        }
    }
    /**
     *
     *
     * @param num
     * @return
     */
    public static String hintBankFourNum(String num) {
        if (num.length()==16){
            //String str1 = num.substring(0, 4);
            String str2 = num.substring(12, num.length());
            return str2;
        }else if (num.length()==19){
            //String str1 = num.substring(0, 4);
            String str2 = num.substring(15, num.length());
            return str2;
        }else {
            return num;
        }
    }
    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i< c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }if (c[i]> 65280&& c[i]< 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    //将字符串转换成Bitmap类型
    public static Bitmap stringtoBitmap(String string){
        Bitmap bitmap=null;
        try {
            byte[]bitmapArray;
            bitmapArray= Base64.decode(string, Base64.DEFAULT);
            bitmap= BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bitmap;
    }


    //将Bitmap转换成字符串
    public static String bitmaptoString(Bitmap bitmap){
        String string=null;
        ByteArrayOutputStream bStream=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100,bStream);
        byte[]bytes=bStream.toByteArray();
        string=Base64.encodeToString(bytes,Base64.DEFAULT);
        return string;
    }
    /**
     * 取北京时间
     * @return
     */
    public static String getBeijingTime(){
        return getFormatedDateString(8);
    }

    /**
     * 取班加罗尔时间
     * @return
     */
    public static String getBangaloreTime(){
        return getFormatedDateString(5.5f);
    }

    /**
     * 取纽约时间
     * @return
     */
    public static String getNewyorkTime(){
        return getFormatedDateString(-5);
    }


    /**
     * 此函数非原创，从网上搜索而来，timeZoneOffset原为int类型，为班加罗尔调整成float类型
     * timeZoneOffset表示时区，如中国一般使用东八区，因此timeZoneOffset就是8
     * @param timeZoneOffset
     * @return
     */
    public static String getFormatedDateString(float timeZoneOffset){
        if (timeZoneOffset > 13 || timeZoneOffset < -12) {
            timeZoneOffset = 0;
        }

        int newTime=(int)(timeZoneOffset * 60 * 60 * 1000);
        TimeZone timeZone;
        String[] ids = TimeZone.getAvailableIDs(newTime);
        if (ids.length == 0) {
            timeZone = TimeZone.getDefault();
        } else {
            timeZone = new SimpleTimeZone(newTime, ids[0]);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        sdf.setTimeZone(timeZone);
        return sdf.format(new Date());
    }

    /**
     * 获取系统当前的时间戳
     *
     * @return
     */
    public static String getTime(){
        long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳
        String  str=String.valueOf(time);
        return str;
    }

    public static String createFileName() {
        String fileName = "";
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
        fileName = dateFormat.format(date) + ".jpg";
        return fileName;
    }

    public synchronized static String getid(Context context) {
        TelephonyManager TelephonyMgr = (TelephonyManager)context.getSystemService(TELEPHONY_SERVICE);
        String ID= TelephonyMgr.getDeviceId();
        return ID;
    }

    /**
     * str empty Validate
     *
     * @param str
     * @param isTrim
     * @return
     */
    public static boolean isEmpty(String str, boolean isTrim) {
        if (str == null || "null".equals(str)) {
            return true;
        }
        if (isTrim) {
            return str.trim().equals(FusionCode.EMT_STR);
        } else {
            return str.equals(FusionCode.EMT_STR);
        }
    }

    public static boolean isEmpty(String str) {
        return isEmpty(str, true);
    }

}
